package com.kichina.parking.upstream;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kichina.parking.exception.extend.JsonParseException;
import com.kichina.parking.service.logic.bean.ManualOpeningRecord;
import com.kichina.parking.service.logic.service.ManualOpeningRecordService;
import com.kichina.parking.utils.JsonToEntityUtil;
import com.kichina.parking.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * @author Surging
 * @version 1.0
 * @ClassName MonthPayUpstreamHandler
 * @date 2022-09-14 20:03
 * @desc 开闸记录业务处理层
 */
@Component
@Slf4j
public class Parking9OpenStrobeUpstreamHandler {

    private final ManualOpeningRecordService manualOpeningRecordService;

    public Parking9OpenStrobeUpstreamHandler(ManualOpeningRecordService manualOpeningRecordService) {
        this.manualOpeningRecordService = manualOpeningRecordService;
    }

    /**
     * @param jsonObject 数据实体
     * @author Surging
     * @Description 手动开闸后主动上传开闸记录处理
     * 上传方式：相隔五分钟检测一次上传。
     * @Return void
     * @date 20:53 2022-09-15
     **/
    public void recordOpenStrobeUpstream(JSONObject jsonObject) throws JsonParseException {
        DateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd");
        ManualOpeningRecord manualOpeningRecord;
        try {
            manualOpeningRecord = JsonToEntityUtil.jsonToEntity(jsonObject, ManualOpeningRecord.class);
            if (Objects.nonNull(jsonObject.getString("opentime"))) {
                if(String.valueOf(jsonObject.getString("opentime")).length()<15){
                    //字符串格式两种 2022-10-15; 2022-09-16 15:54:00;
                    manualOpeningRecord.setOpentimeDate(simpleDateFormat2.parse(String.valueOf(jsonObject.getString("opentime"))));
                }else {
                    manualOpeningRecord.setOpentimeDate(simpleDateFormat.parse(String.valueOf(jsonObject.getString("opentime"))));
                }
            }
            LambdaQueryWrapper<ManualOpeningRecord> eq = new QueryWrapper<ManualOpeningRecord>().lambda()
                    .eq(ManualOpeningRecord::getParkid, manualOpeningRecord.getParkid())
                    .eq(ManualOpeningRecord::getRecordid, manualOpeningRecord.getRecordid())
                    .eq(ManualOpeningRecord::getOpentime, manualOpeningRecord.getOpentime());
            List<ManualOpeningRecord> manualOpeningRecordList = manualOpeningRecordService.list(eq);
            if (CollectionUtils.isNotEmpty(manualOpeningRecordList)){
                //更新
                manualOpeningRecord.setUpdateTime(new Date());
                manualOpeningRecordService.update(manualOpeningRecord, eq);
            }else {
                //添加
                manualOpeningRecord.setManualOpeningRecordId(UUIDUtil.getUUID());
                manualOpeningRecord.setUpdateTime(new Date());
                manualOpeningRecord.setCreateTime(new Date());
                manualOpeningRecordService.save(manualOpeningRecord);
            }
        } catch (ParseException e) {
            log.error("数据格式不合法，解析失败");
            throw new JsonParseException("数据格式不合法，解析失败");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
